<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         *  浅拷贝  
         *  深拷贝
         * 
         */
        var obj = {
            name: 'atguigu',
            sex: 'boy',
            age: {
                realAge: {
                    number: 35
                },
                openAge: {
                    number: 28
                }
            }
        }

        // 拷贝基本数据类型数据拷贝
        var num1 = 1;
        var num2 = num1;
        num2 = 2;
        console.log(num1,num2);

        // 通过赋值拷贝对象？
        var obj2 = obj;// 赋值的是地址
        console.log(obj, obj2);
        obj2.name = '尚硅谷'
        console.log(obj,obj2);

        /**
         * 浅拷贝
         */
        function clone(obj){
            var res = {}
            for(var attr in obj){
                if(obj.hasOwnProperty(attr)){
                    res[attr] = obj[attr];

                    //    attr    obj[attr]   res[attr]
                    //1.  name    atguigu     res['name'] = atguigu
                    //2.  sex     boy         res['sex'] = 'boy'
                    //3.  age     地址        res['age'] = obj['age'] = 地址
                }
            }
            return res;
        }

        var cloneObj= clone(obj);
        cloneObj.name = '尚硅谷666';
        cloneObj.age.realAge.number = 48;
        console.log(cloneObj,obj);





    </script>
</body>

</html>